<template>
  <div class="goodsinfo-container">

    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
  
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:<del>￥{{goodsInfo.market_price}}</del>&nbsp;&nbsp;销售价:<span class="now_price">￥{{goodsInfo.sell_price}}</span>
          </p>
          <p class="numbox">购买数量：<numbox @getcount="getSelectedCount" :max="goodsInfo.stock_quantity"></numbox></p>
          <p class="buy">
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
		</div>
    <!-- 商品参数区域 -->
    <div class="mui-card param">
      <div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：{{goodsInfo.goods_no}}</p>
            <p>库存情况：{{goodsInfo.stock_quantity}}件</p>
            <p>上架时间：{{goodsInfo.add_time|dataFormat}}</p>
					</div>
				</div>
        <div class="mui-card-footer">
          <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
          <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
        </div>
		</div>
  </div>
</template>

<script>
import {Toast} from 'mint-ui'
import swiper from '../subcomponents/swiper.vue'
import numbox from '../subcomponents/goodsinfo_numbox.vue'

export default {
  components:{
    swiper,numbox
  },
  data(){
    return {
      id:this.$route.params.id,
      lunbotu:[],
      goodsInfo:[],
      ballFlag:false,//控制小球隐藏和显示的标识符
      selectedCount:1,//保存用户选中的商品数量，默认为1
    }
  },
  created(){
    this.getThumbs()
    this.getGoodsInfo()
  },
  methods:{
    getThumbs(){//获取缩略图
            this.$axios({
            method:'get',
            url:'/api/getthumimages/'+this.id,
        }).then((response) =>{          //这里使用了ES6的语法
            //console.log(response)       //请求成功返回的数据
            if(response.data.status===0){
                response.data.message.forEach(item => {
                    item.img = item.src
                });
                this.lunbotu = response.data.message
            }else{
                Toast('获取缩略图失败！')
            }
        }).catch((error) =>{
            console.log(error)       //请求失败返回的数据
        })
    },
    getGoodsInfo(){//获取商品详情
        this.$axios({
            method:'get',
            url:'/api/goods/getinfo/'+this.id,
        }).then((response) =>{          //这里使用了ES6的语法
            //console.log(response)       //请求成功返回的数据
            if(response.data.status===0){
                this.goodsInfo = response.data.message[0]
            }else{
                Toast('获取商品详情失败！')
            }
        }).catch((error) =>{
            console.log(error)       //请求失败返回的数据
        })
    },
    goDesc(id){//使用编程式导航跳转到图文介绍页面
      this.$router.push({name:'goodsdesc',params:{id}})
    },
    goComment(id){//使用编程式导航跳转到商品评论页面
      this.$router.push({name:'goodscomment',params:{id}})
    },
    addToShopCar(){//添加到购物车
      this.ballFlag = !this.ballFlag
      //{id:商品id,count:要购买的数量,price:商品单价,selected:false}中car数组中的对象
      //拼接出一个要保存到store
      var goodsinfo = {
        id:this.id,
        count:this.selectedCount,
        price:this.goodsInfo.sell_price,
        selected:false
      }
      //调用store中mutations的addToCar方法，将商品加入购物车
      this.$store.commit('addToCar',goodsinfo)
    },
    beforeEnter(el){
      el.style.transform = "translate(0,0)"
    },
    enter(el,done){
      //获取小球在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect()
      //获取徽标在页面中的位置
      const badgePosition = document.getElementById('badge').getBoundingClientRect()

      const xDist = badgePosition.left - ballPosition.left
      const yDist = badgePosition.top - ballPosition.top

      el.offsetWidth
      el.style.transform = `translate(${xDist}px,${yDist}px)`
      el.style.transition = 'all 0.8s cubic-bezier(.4,-0.3,1,.68)'
      done()
    },
    afterEnter(){
      this.ballFlag = !this.ballFlag
    },
    getSelectedCount(count){
      //当子组件把选中的数量传递给父组件的时候，把选中的值保存到data
      this.selectedCount = count
      console.log(this.selectedCount)
    }
  }
}
</script>

<style lang="scss" scoped>

.goodsinfo-container{
  background-color: #ddd;
  overflow: hidden;
  .now_price{
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .buy{
    button{
      margin-right: 6px;
    }
  }
  .numbox{
    display: flex;
    align-items: center;
  }
  .mui-card-header:after{
    background-color: #999;
  }
  .param{
    .mui-card-header:after{
      background-color: #333;
    }
    .mui-card-footer{
      display: block;
      button{
        margin: 15px 0;
      }
    }
    .mui-card-footer:before{
      background-color: #545151;
    }
  }
  .ball{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    top:386px;
    left: 146px;
    z-index: 99;
  }
}

</style>
